package com.oms.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.liudecai.utils.jdbc.TxQueryRunner;
import com.oms.bean.Order;

public class OrderDao {
	QueryRunner qr = null;

	public OrderDao() {
		qr = new TxQueryRunner();
	}

	public void Add(Order order) throws SQLException {
		String sql = "insert into oms_order(user_id,food_id,food_number,food_price,total_price,order_time,whether_send) values(?,?,?,?,?,?,?)";
		qr.update(sql, order.getUser_id(), order.getFood_id(), order.getFood_number(), order.getFood_price(),
				order.getTotal_price(), order.getOrder_time(), order.getWhether_send());
	}

	public void Delete(int id) throws SQLException {
		String sql = "delete from oms_order where id=?";
		qr.update(sql, id);
	}

	public void Update(Order order) throws SQLException {
		String sql = "update oms_order set user_id=?,food_id=?,food_number=?,food_price=?,total_price=?,order_time=?,whether_send=? where id=?";
		qr.update(sql, order.getFood_id(),order.getFood_id(), order.getFood_number(), order.getFood_price(),
				order.getTotal_price(), order.getOrder_time(), order.getWhether_send(),order.getId());
	}

	public Order FindById(int id) throws SQLException {
		String sql = "select * from oms_order where id=?";
		return qr.query(sql, new BeanHandler<Order>(Order.class),id);
	}

	public List<Order> FindByString(String str) throws SQLException {
		String sql = "";
		qr.query(sql, new BeanListHandler<Order>(Order.class));
		return null;

	}

	public List<Order> FindAll() throws SQLException {
		String sql = "select * from oms_order";
		return qr.query(sql, new BeanListHandler<Order>(Order.class));
	}

	public long OrderCount() throws SQLException {
		String sql = "select count(id) from oms_order";
		return qr.query(sql, new ScalarHandler<>());
	}

}
